package com.yangjun.common.excel;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
@Component
public class DataBaseExcelContext {
	

	@Autowired
	private DataBaseExcelDefinitionReader dataBaseExcelDefinitionReader;
	private ExcelImport excelImport;
	private ExcelExport excelExport;
	private Map<String,String> excelMap = null;
	public void initDBExcelDefinitionReader(String type){
		this.excelMap = dataBaseExcelDefinitionReader.init(type);
		excelImport = new ExcelImport(dataBaseExcelDefinitionReader);
		excelExport = new ExcelExport(dataBaseExcelDefinitionReader);
	}
	public String queryExcelName(String excelId){
		return this.excelMap.get(excelId);
	}
	public List<String> queryExcelIdList(){
		List<String> excelIdList = Lists.newArrayList();
		Map<String,ExcelDefinition> excelExcelDefinitionMap =  dataBaseExcelDefinitionReader.getRegistry();
		for(Map.Entry<String , ExcelDefinition> ExcelDefinitionEntry:excelExcelDefinitionMap.entrySet()){
			excelIdList.add(ExcelDefinitionEntry.getKey());
		}
		return excelIdList;
	}
	
	/***
	 * 读取Excel信息
	 * @param id 配置ID
	 * @param titleIndex 标题索引,从0开始
	 * @param excelStream Excel文件流
	 * @return ExcelImportResult
	 * @throws Exception 
	 */
	public ExcelImportResult readExcel(String id,int titleIndex, InputStream excelStream) throws Exception {
		return excelImport.readExcel(id,titleIndex, excelStream,null);
	}
	/**
	 * 
	 * @Description 创建Excel
	 * @param id 配置的ID
	 * @param beans 对应配置CLASS的List
	 * @return
	 * @author yangjun
	 * @Date 2017年6月23日 上午9:35:05
	 */
	public Workbook createExcel(String id,List<?> beans){
		return createExcel(id,beans,null);
	}
	/**
	 * @Description (TODO这里用一句话描述这个方法的作用)
	 * @param id 配置的ID
	 * @param beans 对应配置CLASS的List
	 * @param fields 指定导出的字段
	 * @return
	 * @author yangjun
	 * @Date 2017年6月23日 上午9:36:27
	 */
	public Workbook createExcel(String id,List<?> beans,List<String> fields){
		return excelExport.createExcel(id, beans,fields).build();
	}
	/**
	 * 创建Excel部分信息
	 * @param id 配置ID
	 * @param beans 配置class对应的List
	 * @return Workbook
	 * @throws Exception 
	 */
	public ExcelExportResult createExcelForPart(String id, List<?> beans,List<String> fields) throws Exception {
		return excelExport.createExcel(id, beans,fields);
	}
	public ExcelExportResult createEmptyExcelForPart(String id, Class<?> clazz,List<String> fields) throws Exception {
		return excelExport.createEmptyExcel(id, clazz,fields);
	}
	public ExcelImport getExcelImport() {
		return excelImport;
	}

	public void setExcelImport(ExcelImport excelImport) {
		this.excelImport = excelImport;
	}

	public ExcelExport getExcelExport() {
		return excelExport;
	}

	public void setExcelExport(ExcelExport excelExport) {
		this.excelExport = excelExport;
	}

}
